#ifndef RoRequest__h
#define RoRequest__h


#include <QtCore>
#include <QtXml>

#include "RoAbstractComponent.h"


class RoQuery:
    public QObject
{
    Q_OBJECT

    Q_PROPERTY( QString remoteComponentName
                READ remoteComponentName
                WRITE setRemoteComponentName );

    Q_PROPERTY( QString remoteMethod
                READ remoteMethod
                WRITE setRemoteMethod );

    Q_PROPERTY( RoStanzaId rid
                READ rid );

public:

    typedef QObject super;

    RoQuery(RoAbstractComponent * parent = 0);

    RoAbstractComponent * parentComponent() const
    {
        return qobject_cast<RoAbstractComponent*>(parent());
    }

    QString remoteComponentName() const
        { return _remoteComponentName; }
    void setRemoteComponentName(QString name)
        { _remoteComponentName = name; }

    QString remoteMethod() const
        { return _remoteMethod; }
    void setRemoteMethod(QString method)
        { _remoteMethod = method; }

    RoStanzaId rid() const
        { return _rid; }

    void setDataAcceptor(QObject * obj, const char * method)
    {
        dataAcceptor = obj;
        dataAcceptorMethod = method;
    }

    void setResultAcceptor(QObject * obj, const char * method)
    {
        resultAcceptor = obj;
        resultAcceptorMethod = method;
    }

public slots:

    /// Send request
    bool exec(QDomDocument argument = QDomDocument());

    /// Receive a datum.
    void datum(RoIqStanza stanza);

    /// End query with result or error.
    void end(RoIqStanza stanza);

private:

    QString _remoteComponentName;
    QString _remoteMethod;
    RoStanzaId _rid;

    QPointer<QObject> dataAcceptor;
    const char * dataAcceptorMethod;

    QPointer<QObject> resultAcceptor;
    const char * resultAcceptorMethod;

signals:

    void requestSent(RoStanzaId rid);

    void query_datum(QDomDocument datum);
    void query_result(bool error, QDomDocument answer);

}; // class RoQuery


#endif // RoRequest__h
